﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;

namespace XCI.Windows
{
    /// <summary>
    /// 选项卡控件动态加载控制(只有第一次激活时才执行代码)
    /// </summary>
    public class DynamicLoadPage
    {
        /// <summary>
        /// 已经加载的页面
        /// </summary>
        private readonly IList<string> LoadPages = new List<string>();

        /// <summary>
        /// 初始化选项卡控件加载(在SelectedPageChanging事件中添加语句LoadPage(pageName))
        /// </summary>
        /// <param name="pageLoadAction">页面加载</param>
        public DynamicLoadPage(Action<string> pageLoadAction)
        {
            this.PageLoadAction = pageLoadAction;
        }
        
        private Action<string> PageLoadAction { get; set; }
        
        /// <summary>
        /// 加载Tab页
        /// </summary>
        /// <param name="pageName">Tab页名称</param>
        public void LoadPage(string pageName)
        {
            string name = pageName;
            if (LoadPages.Any(p => p.Equals(name))) return;

            PageLoadAction?.Invoke(name);
            LoadPages.Add(name);
        }

        /// <summary>
        /// 清空加载状态
        /// </summary>
        public void Clear()
        {
            LoadPages.Clear();
        }
    }
}